﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialPlatformSDK.Deserializer;
using SocialPlatformSDK.Hammock;
using SocialPlatformSDK.Model;
using SocialPlatformSDK.Services;
using SocialPlatformSDK.Services.Util;

namespace SocialPlatformSDK
{
    /// <summary>
    /// TencentAPIService 包含了 微博 相关 API.
    /// T 是 ServiceArgument
    /// </summary>
    public class SinaAPIService<T> : BaseService<T>
        where T : ServiceArgument, new()
    {

        public SinaAPIService(AccessToken accessToken, IOAuthConfigruation oAuthConfigruationBase)
            : base(accessToken, oAuthConfigruationBase)
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        public SinaAPIService()
            : base(null, null)
        { }

        private bool ConverArgument(ref ServiceArgument argment)
        {
            bool res = false;
            SinaServiceArgument arg = argment as SinaServiceArgument;
            if (arg != null)
            {
                res = true;
                //arg.Oauth_Consumer_Key = this.oAuthConfigruationBase.APP_KEY;
                arg.AccessToken = this.oAuthConfigruationBase.AccessToken;
                //arg.OpenId = this.oAuthConfigruationBase.AccessToken.Openid;
                // arg.ClientIp = "117.135.159.4";
            }
            return res;
        }

   
        /// <summary>
        /// 发表一条带图片的微博.
        /// </summary>
        /// <param name="argment">参数列表，Content和UploadPic为必填参数.</param>
        /// <param name="callback">无返回数据.</param>
        public void AddPic(ServiceArgument argment, Action<Callback<object>> callback)
        {
            this.Post("t/add_pic", argment, (request, response, userState) =>
            {
                if (null != callback)
                {
                    callback(new Callback<object>(null));
                }
            });
        }


    


        /// <summary>
        /// 主页时间线.
        /// </summary>
        /// <param name="argment">参数列表, 可以为 null.</param>
        /// <param name="callback">回调返回主页时间线数据.</param>
        public void HomeTimeline(ServiceArgument argment, Action<Callback<StatusCollection>> callback)
        {
            if (!ConverArgument(ref argment))
                return;
            this.Get("/statuses/friends_timeline/ids.json", argment, (request, response, userState) =>
            {
                InternalCallback(response, callback);
            });
        }




        private void InternalCallback(RestResponse response, Action<Callback<StatusCollection>> callback)
        {
            lock (this)
            {
                StatusCollection list = null;

                BaseDeserializer serializer = DeserializerManager.Instance.BuildStatusDeserializer();
                list = serializer.ReadList(response.Content) as StatusCollection;

                if (null != callback)
                {
                    callback(new Callback<StatusCollection>(list));
                }
            }
        }
    }
}
